package com.xkdx.caipiao;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.xkdx.caipiao.listener.OnAlertDialogOkListener;
import com.xkdx.caipiao.module.network.AbsModule;
import com.xkdx.caipiao.presistence.login.Login;
import com.xkdx.caipiao.presistence.login.LoginInfo;
import com.xkdx.caipiao.presistence.versioninfo.VersionInfoAction;
import com.xkdx.caipiao.presistence.versioninfo.VersionInfoInfo;
import com.xkdx.caipiao.presistence.versioninfo.VersionInfoModule;
import com.xkdx.caipiao.presistence.versioninfo.VersionInfoPresistence;
import com.xkdx.caipiao.setup.AboutActivtiy;
import com.xkdx.caipiao.setup.AlterPasswordActivity;
import com.xkdx.caipiao.setup.HelpActivtiy;
import com.xkdx.caipiao.setup.NotificationActivtiy;
import com.xkdx.caipiao.setup.PushActivtiy;
import com.xkdx.caipiao.setup.ShowIdentityInfo;
import com.xkdx.caipiao.statics.IConstants;
import com.xkdx.caipiao.util.SharePrefenceUtil;
import com.xkdx.caipiao.wiget.MyAlertDialog;

public class SetupActivity extends ThreadActivity implements OnClickListener,
		OnAlertDialogOkListener {
	private Button exit;
	private RelativeLayout userinfo, noticeinfo, pushinfo, versioninfo,
			aboutinfo, adviceinfo, helpinfo,xg_password;
	private SharePrefenceUtil su;
	private VersionInfoModule module;
	private VersionInfoAction action;
	private VersionInfoPresistence presistence;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setup_caipiao);

		findView();

	}

	@Override
	protected void onResume() {
		super.onResume();
		LoginInfo info = getLoginInfo();
		if (info != null) {
			exit.setVisibility(View.VISIBLE);
			exit.setText("退出");
		} else {
			exit.setVisibility(View.GONE);

		}
	}

	private void findView() {
		exit = (Button) findViewById(R.id.exit);

		userinfo = (RelativeLayout) findViewById(R.id.rl_userinfo);
		noticeinfo = (RelativeLayout) findViewById(R.id.rl_noticeinfo);
		pushinfo = (RelativeLayout) findViewById(R.id.rl_pushinfo);
		versioninfo = (RelativeLayout) findViewById(R.id.rl_versioninfo);
		aboutinfo = (RelativeLayout) findViewById(R.id.rl_aboutusinfo);
		adviceinfo = (RelativeLayout) findViewById(R.id.rl_adviceinfo);
		helpinfo = (RelativeLayout) findViewById(R.id.rl_helpinfo);
		xg_password = (RelativeLayout) findViewById(R.id.xg_password);
		
		userinfo.setOnClickListener(this);
		noticeinfo.setOnClickListener(this);
		pushinfo.setOnClickListener(this);
		versioninfo.setOnClickListener(this);
		aboutinfo.setOnClickListener(this);
		adviceinfo.setOnClickListener(this);
		exit.setOnClickListener(this);
		helpinfo.setOnClickListener(this);
		xg_password.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_userinfo:
			su = new SharePrefenceUtil(SetupActivity.this,
					SharePrefenceUtil.USERINFO);
			if (su.getUserInfo() != null) {
				LoginInfo user = su.getUserInfo();
				if (user != null) {
					if (user.getVerify().equals("1")) {
						Intent identity = new Intent(SetupActivity.this,
								ShowIdentityInfo.class);
						startActivity(identity);
					} else {
						Intent intent3 = new Intent(SetupActivity.this,
								CompleteUserInfoActivity.class);
						startActivity(intent3);
					}
				}

			} else {
				setLogin();
			}

			break;
		case R.id.xg_password:
			su = new SharePrefenceUtil(SetupActivity.this,
					SharePrefenceUtil.USERINFO);
			if (su.getUserInfo() != null) {
				LoginInfo user = su.getUserInfo();
				if (user != null) {
				
						Intent identity = new Intent(SetupActivity.this,
								AlterPasswordActivity.class);
						startActivity(identity);
						/*InputMethodManager im = (InputMethodManager)getSystemService(AlterPasswordActivity.INPUT_METHOD_SERVICE); 
						im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);*/
				}

			} else {
				setLogin();
			}

			
			break;
		case R.id.rl_noticeinfo:
			Intent notice = new Intent(SetupActivity.this,
					NotificationActivtiy.class);
			startActivity(notice);
			break;
		case R.id.rl_pushinfo:
			Intent push = new Intent(SetupActivity.this, PushActivtiy.class);
			startActivity(push);
			break;
		case R.id.rl_versioninfo:
			startThread();
			break;
		case R.id.rl_aboutusinfo:
			Intent about = new Intent(SetupActivity.this, AboutActivtiy.class);
			startActivity(about);
			break;
		case R.id.rl_adviceinfo:
			Intent intent = new Intent(this, SuggestActivity.class);
			startActivity(intent);
			break;
		case R.id.rl_helpinfo:
			Intent help = new Intent(SetupActivity.this, HelpActivtiy.class);
			startActivity(help);
			break;

		case R.id.exit:
			LoginInfo info = getLoginInfo();
			if (info != null) {
				showExit();
			} else {
				Intent login = new Intent(this, Login.class);
				startActivity(login);
			}
			break;

		}
	}

	private void showExit() {
		MyAlertDialog.creatAlertDialog(SetupActivity.this, "退出提示", "确定退出账号?");
		MyAlertDialog.setOnAlertDialogOklistener(this);
		SharePrefenceUtil sp = new SharePrefenceUtil(this, SharePrefenceUtil.USERINFO);
		sp.clear();
		IConstants.list_selectcode.clear();
		IConstants.list_sevencolor.clear();
		IConstants.list_three.clear();
		IConstants.list_five.clear();

	}

	private void download(String clientDownloadUrl) {
		IConstants.download(this, clientDownloadUrl);
	}
	
	@Override
	public void showOnPost(HashMap<String, AbsModule> result) {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		if (result.containsKey("0")) {
			ArrayList<VersionInfoInfo> list = module.list;
			if(module.list.size() > 0){
				final String urlStr = module.list.get(0).getUrl();
				if(urlStr.length() > 0){
					AlertDialog.Builder builder = new AlertDialog.Builder(
							this);
					builder.setTitle("有新版本,是否更新");
					builder.setNegativeButton("是",
							new DialogInterface.OnClickListener() {
								@Override
								public void onClick(DialogInterface dialog,
										int which) {
									download(urlStr);
								}
							});
					builder.setPositiveButton("否", null);
					builder.create().show();
				}else{
					Toast.makeText(getApplicationContext(), "已经是最新版本", 0).show();
				}
			}
		}else{
			Toast.makeText(getApplicationContext(), "已经是最新版本", 0).show();
		}
	}

	@Override
	public void startThread() {
		if (dataLoadDialog.isShowing()) {
			dataLoadDialog.dismiss();
		}
		action = new VersionInfoAction("",
				"");
		module = new VersionInfoModule();
		presistence = new VersionInfoPresistence(this);
		presistence.setActitons(action);
		presistence.setModule(module);
		presistence.execute();


	}

	@Override
	public void handleOkClick() {
		SharePrefenceUtil su = new SharePrefenceUtil(this,
				SharePrefenceUtil.USERINFO);
		su.clear();
		MyLotterActivity.loginInfo = null;
		exit.setVisibility(View.GONE);
		Intent inent = new Intent(SetupActivity.this, HomePageActivity.class);
		inent.putExtra("tag", "setup");
		startActivity(inent);
	}

	@Override
	public void handleDismissClick() {

	}

	@Override
	public void onBackClick(View view) {
		Log.i("hz", "");

	}

}
